import { useEffect, useState } from 'react';
import { message } from 'antd';
import { getInmails, getSentmails } from '@/services/mail';

// 收件箱邮件列表
const useMailList = query => {
    const [mailListLoading, setMailListLoading] = useState(false);
    const [mailListResData, setMailListResData] = useState([]);
    const [mailListTotal, setMailListTotal] = useState(0);
    const [mailListResCode, setMailListResCode] = useState(null);

    const fetchData = async () => {
        setMailListLoading(true);
        const { belongs, pageSize, pageNo } = query;
        const playload = { belongs, pageSize, pageNo }; // 查询参数
        const { code, data, msg } = await (query.mailType === 'in' ? getInmails(playload) : getSentmails(playload));
        if (code === 10000) {
            setMailListResData(data.rows);
            setMailListTotal(data.total);
        } else if (msg && msg.length > 0 && code !== 20025) {
            message.error(msg);
        }
        setMailListResCode(code);
        setMailListLoading(false);
    };
    useEffect(() => {
        if (query.belongs && query.belongs !== '') {
            fetchData();
        }
    }, [query]);
    return { mailListLoading, mailListResData, mailListTotal, setMailListResData, mailListResCode };
};

export default useMailList;
